/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package uk.co.dupenois.geometry;

import java.util.Vector;

/**
 *
 * @author mpd209
 */
public class Rectangle {

    private Line top;
    private Line left;
    private Line right;
    private Line bottom;

    private double width;
    private double height;

    public Rectangle(Coordinate bottomLeft, double width, double height) {
        Coordinate topLeft = new Coordinate(bottomLeft.getX(), bottomLeft.getY()+height);
        Coordinate topRight = new Coordinate(topLeft.getX()+width, topLeft.getY());
        Coordinate bottomRight = new Coordinate(topLeft.getX()+width, topLeft.getY()-height);
//        Coordinate topRight = new Coordinate(topLeft.getX()+width, topLeft.getY());
//        Coordinate bottomRight = new Coordinate(topLeft.getX()+width, topLeft.getY()-height);
//        Coordinate bottomLeft = new Coordinate(topLeft.getX(), topLeft.getY()-height);
        this.top = new Line(topLeft, topRight, true);
        this.right = new Line(topRight, bottomRight, true);
        this.bottom = new Line(bottomRight, bottomLeft, true);
        this.left = new Line(bottomLeft, topLeft, true);
        this.width = width;
        this.height = height;
    }


    public boolean isSquare(){
        return (getWidth() == getHeight());
    }
 
    public double getArea() {
        return getWidth()*getHeight();
    }

    public Shape getShape(){
        Shape shape = new Shape();
        shape.addLine(top);
        shape.addLine(right);
        shape.addLine(bottom);
        shape.addLine(left);
        return shape;
    }

    public Coordinate crossesTop(Line line){
        return top.coincides(line);
    }
    public Coordinate crossesBottom(Line line){
        return bottom.coincides(line);
    }
    public Coordinate crossesLeft(Line line){
        return left.coincides(line);
    }
    public Coordinate crossesRight(Line line){
        return right.coincides(line);
    }
    public Coordinate[] crossesMe(Line line){
        Vector<Coordinate> collisions = new Vector<Coordinate>();
        Coordinate collision;
        if((collision = crossesTop(line))!=null)collisions.add(collision);
        if((collision = crossesRight(line))!=null)collisions.add(collision);
        if((collision = crossesLeft(line))!=null)collisions.add(collision);
        if((collision = crossesBottom(line))!=null)collisions.add(collision);
        return collisions.toArray(new Coordinate[collisions.size()]);
    }
    public boolean inMe(Line line){
        Coordinate start = line.getStart();
        Coordinate finish = line.getFinish();
        double minX = this.top.getStart().getX();
        double maxX = this.top.getFinish().getX();
        double minY = this.left.getStart().getY();
        double maxY = this.left.getFinish().getY();
        return(start.getX()>minX&&start.getX()<maxX&&
                finish.getX()>minX&&finish.getX()<maxX&&
                start.getY()>minY&&start.getY()<maxY&&
                finish.getY()>minY&&finish.getY()<maxY);
    }

    public boolean intersectsWithMe(Line line){
        boolean intersects = (inMe(line)||crossesMe(line).length>0);
//        if(intersects){
//            System.out.println("-----------");
//            System.out.println("Me "+this);
//            System.out.println("Line "+line.getStart().toString(1)+" -- "+line.getFinish().toString(1));
//            System.out.println("In Me: "+inMe(line));
//            System.out.println("Crosses Me at: "+crossesMe(line));
//            System.out.println("-----------");
//        }
        return intersects;
    }

    /**
     * @return the top
     */
    public Line getTop() {
        return top;
    }

    /**
     * @return the left
     */
    public Line getLeft() {
        return left;
    }

    /**
     * @return the right
     */
    public Line getRight() {
        return right;
    }

    /**
     * @return the bottom
     */
    public Line getBottom() {
        return bottom;
    }

    /**
     * @return the width
     */
    public double getWidth() {
        return width;
    }

    /**
     * @return the height
     */
    public double getHeight() {
        return height;
    }

    @Override
    public String toString(){
        String string = "("+top.getStart().toString(1)+", "+
                top.getFinish().toString(1)+", "+
                right.getFinish().toString(1)+", "+
                bottom.getFinish().toString(1)+")";
        return string;
    }

}
